//
//  MallHomePageViewController.m
//  LiteraryTune
//
//  Created by 神州四达 on 2018/1/18.
//  Copyright © 2018年 cxd. All rights reserved.
//  首页

#import "MallHomePageViewController.h"
#import "BookCollectCell.h"
#import "RecommendCollectionReusableView.h"
#import "RankTableViewCell.h"
#import "RankHeaderView.h"
#import "BookDetailShowViewController.h"
#import "BookListModel.h"
#import "BookDetailModel.h"
#import "RecommendBookVC.h"
#import "FooterCollectionReusableView.h"
#import "RankBookVC.h"
#import "BookNeewVC.h"

@interface MallHomePageViewController () <UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic, strong)UICollectionView *recommentCollectionView;

//数据源
//推荐图书
@property (nonatomic, strong)NSArray *recommentArray;
//阅读榜
@property (nonatomic, strong)NSArray *rankArray;
//新书上架
@property (nonatomic, strong)NSArray *BookNewArray;

@end

@implementation MallHomePageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self getNetData];
    [self getRankNetData];
    [self getNewBookNetData];
    [self buildUI];
}

- (void)getNetData {
    [CXDNetHelp getRequestWithURLString:RecomendBookURL parameters:nil success:^(id  _Nullable responseObject) {
        NSLog(@"%@   %@",RecomendBookURL,responseObject);
        _recommentArray = [NSArray yy_modelArrayWithClass:[BookDetailModel class] json:responseObject];
        [self.recommentCollectionView reloadData];
    } failure:^(NSError * _Nullable error) {
        
    }];
}

- (void)getRankNetData {
    [CXDNetHelp getRequestWithURLString:ReadRankURL parameters:nil success:^(id  _Nullable responseObject) {
        _rankArray = [NSArray yy_modelArrayWithClass:[BookDetailModel class] json:responseObject];
        [self.recommentCollectionView reloadData];
    } failure:^(NSError * _Nullable error) {
        
    }];
}

- (void)getNewBookNetData {
    [CXDNetHelp getRequestWithURLString:NewBookShelfURL parameters:nil success:^(id  _Nullable responseObject) {
        _BookNewArray = [NSArray yy_modelArrayWithClass:[BookDetailModel class] json:responseObject];
        [self.recommentCollectionView reloadData];
    } failure:^(NSError * _Nullable error) {
        
    }];
}

- (void)buildUI {
    [self.view addSubview:self.recommentCollectionView];
}

- (void)moreBtnAction:(UIButton *)sender {
    if (sender.indexPath.section == 0) {
        RecommendBookVC *bookVC = [[RecommendBookVC alloc] init];
        [self.navigationController pushViewController:bookVC animated:YES];
    }else if (sender.indexPath.section == 1) {
        RankBookVC *bookVC = [[RankBookVC alloc] init];
        [self.navigationController pushViewController:bookVC animated:YES];
    }else if (sender.indexPath.section == 2) {
        BookNeewVC *bookVC = [[BookNeewVC alloc] init];
        [self.navigationController pushViewController:bookVC animated:YES];
    }
}

#pragma mark - 懒加载
- (UICollectionView *)recommentCollectionView {
    if (!_recommentCollectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.minimumLineSpacing = 5;
        layout.minimumInteritemSpacing = 5;
        layout.itemSize = CGSizeMake((kScreenWidth - 10) / 3, 150);
        layout.headerReferenceSize = CGSizeMake(kScreenWidth, 80);
        layout.footerReferenceSize = CGSizeMake(kScreenWidth, 65);
        _recommentCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0,10, kScreenWidth, kScreenHeight - CXDTopHeight - 49 - 60) collectionViewLayout:layout];
        _recommentCollectionView.delegate = self;
        _recommentCollectionView.dataSource = self;
        _recommentCollectionView.backgroundColor = [UIColor colorFromHexCode:@"#eeeeee"];
        _recommentCollectionView.showsVerticalScrollIndicator = NO;
        [_recommentCollectionView registerNib:[UINib nibWithNibName:NSStringFromClass([BookCollectCell class]) bundle:nil] forCellWithReuseIdentifier:NSStringFromClass([BookCollectCell class])];
        [_recommentCollectionView registerNib:[UINib nibWithNibName:NSStringFromClass([FooterCollectionReusableView class]) bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:NSStringFromClass([FooterCollectionReusableView class])];
        [_recommentCollectionView registerNib:[UINib nibWithNibName:NSStringFromClass([RecommendCollectionReusableView class]) bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass([RecommendCollectionReusableView class])];
    }
    return _recommentCollectionView;
}

#pragma mark - 集合视图协议实现
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 3;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (section == 0) {
        if (_recommentArray.count > 6) {
            return 6;
        }
        return _recommentArray.count;
    }else if (section == 1) {
        if (_rankArray.count > 6) {
            return 6;
        }
        return _rankArray.count;
    }else if (section == 2) {
        if (_BookNewArray.count > 6) {
            return 6;
        }
        return _BookNewArray.count;
    }
    return 6;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    BookCollectCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([BookCollectCell class]) forIndexPath:indexPath];
    cell.backView.hidden = YES;
    BookDetailModel *tempModel;
    if (indexPath.section == 0) {
        tempModel = _recommentArray[indexPath.item];
    }else if (indexPath.section == 1) {
        tempModel = _rankArray[indexPath.item];
    }else if (indexPath.section == 2) {
        tempModel = _BookNewArray[indexPath.item];
    }
    [cell.bookImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",RootHomeURL,tempModel.spic]]];
    cell.nameLabel.text = tempModel.news_tit;
    return cell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        RecommendCollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:NSStringFromClass([RecommendCollectionReusableView class]) forIndexPath:indexPath];
        if (indexPath.section == 0) {
            view.titleLabel.text = @"推荐图书";
            view.subLabel.text = @"Recommended Books";
        }else if (indexPath.section == 1) {
            view.titleLabel.text = @"排行榜";
            view.subLabel.text = @"Ranking List";
        }else if (indexPath.section == 2) {
            view.titleLabel.text = @"新书上架";
            view.subLabel.text = @"New books";
        }
        return view;
    }
    FooterCollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:NSStringFromClass([FooterCollectionReusableView class]) forIndexPath:indexPath];
    view.moreBtn.indexPath = indexPath;
    [view.moreBtn addTarget:self action:@selector(moreBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    return view;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    BookDetailShowViewController *detailVC = [[BookDetailShowViewController alloc] init];
    BookDetailModel *tempModel;
    if (indexPath.section == 0) {
        tempModel = _recommentArray[indexPath.item];
    }else if (indexPath.section == 1) {
        tempModel = _rankArray[indexPath.item];
    }else if (indexPath.section == 2) {
        tempModel = _BookNewArray[indexPath.item];
    }
    detailVC.navigationItem.title = tempModel.news_tit;
    detailVC.bookId = tempModel.bookID;
    [self.navigationController pushViewController:detailVC animated:YES];
}

@end
